package com.symaster.common.swing;

import javax.swing.*;
import java.awt.event.*;

/**
 * @author yinmiao
 * @version 2022-11-05 16:32
 */
public abstract class KeyStatus extends JComponent {

    public Boolean leftCtrlPressed;
    public Boolean rightCtrlPressed;
    public Boolean leftShiftPressed;
    public Boolean rightShiftPressed;
    public Boolean leftAltPressed;
    public Boolean rightAltPressed;
    public Boolean leftMouseButtonPressed;
    public Boolean rightMouseButtonPressed;
    public Boolean centerMouseButtonPressed;

    public KeyStatus() {
        allPressed();

        initListener();
        this.requestFocus();
    }

    private void initListener() {
        this.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (17 == e.getKeyCode()) {
                    if (2 == e.getKeyLocation()) {
                        leftCtrlPressed = true;
                    }
                    if (3 == e.getKeyLocation()) {
                        rightCtrlPressed = true;
                    }
                    return;
                }
                if (16 == e.getKeyCode()) {
                    if (2 == e.getKeyLocation()) {
                        leftShiftPressed = true;
                    }
                    if (3 == e.getKeyLocation()) {
                        rightShiftPressed = true;
                    }
                    return;
                }
                System.out.println(e.getKeyCode());
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (17 == e.getKeyCode()) {
                    if (2 == e.getKeyLocation()) {
                        leftCtrlPressed = false;
                    }
                    if (3 == e.getKeyLocation()) {
                        rightCtrlPressed = false;
                    }
                }
                if (16 == e.getKeyCode()) {
                    if (2 == e.getKeyLocation()) {
                        leftShiftPressed = false;
                    }
                    if (3 == e.getKeyLocation()) {
                        rightShiftPressed = false;
                    }
                }
            }
        });
        this.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                allPressed();
            }
        });
        this.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (1 == e.getButton()) {
                    leftMouseButtonPressed = true;
                }
                if (2 == e.getButton()) {
                    centerMouseButtonPressed = true;
                }
                if (3 == e.getButton()) {
                    rightMouseButtonPressed = true;
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (1 == e.getButton()) {
                    leftMouseButtonPressed = false;
                }
                if (2 == e.getButton()) {
                    centerMouseButtonPressed = false;
                }
                if (3 == e.getButton()) {
                    rightMouseButtonPressed = false;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    public void allPressed() {
        leftCtrlPressed = false;
        rightCtrlPressed = false;
        leftShiftPressed = false;
        rightShiftPressed = false;
        leftAltPressed = false;
        rightAltPressed = false;
        leftMouseButtonPressed = false;
        rightMouseButtonPressed = false;
        centerMouseButtonPressed = false;
    }
}
